package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import com.atomos.log.AtomosDebug;
	import com.atomos.particle.Particle;
	import com.atomos.particle.ParticleManager;
	import com.atomos.strategy.action.StackAction;
	import com.atomos.strategy.action.life.LifeAction;
	import com.atomos.strategy.action.position.MoveAction;
	import com.atomos.strategy.action.velocity.ForceAction;
	import com.atomos.strategy.action.velocity.FrictionAction;
	import com.atomos.strategy.emission.ByRateEmission;
	import com.atomos.strategy.initialize.StackInitializer;
	import com.atomos.strategy.initialize.life.LifeInitializer;
	import com.atomos.strategy.initialize.velocity.ExplosionInitializer;
	import com.atomos.system.SimpleParticleSystem;
	import com.atomos.utils.AtomosMonitorFieldsList;
	import com.bourre.collection.Iterator;
	import com.bourre.log.Logger;
	import com.bourre.structures.Range;
	import com.bourre.utils.FlashInspectorLayout;
	import com.kairos.engine.RTBeacon;
	import com.kairos.utils.Monitor;
	import com.kairos.utils.MonitorFieldsList;	

	[SWF(width="550",height="400",frameRate="40",backgroundColor="#000000")]
	public class FountainExample extends Sprite
	{
		protected var _system : SimpleParticleSystem;
		
		public function FountainExample ()
		{
			Logger.getInstance().addLogListener( FlashInspectorLayout.getInstance(), AtomosDebug.CHANNEL );
			
			// Set the maximum step for the current animation
			RTBeacon.getInstance().setMaxStepSize( 100 );
			
			// Creating a monitor 
			var monitor : Monitor = new Monitor( 140, 60 );
			monitor.addField( MonitorFieldsList.FPS );
			monitor.addField( MonitorFieldsList.MEMORY );
			monitor.addField( AtomosMonitorFieldsList.PARTICLES );
			monitor.start();
			addChild ( monitor );
			
			// Creating the stack for particles action
			var action : StackAction = new StackAction();
			action.add( new LifeAction() );
			action.add( new ForceAction ( new Point ( 0, 90 ) ) );
			action.add( new ForceAction ( new Point ( 30, 0 ) ) );
			action.add( new FrictionAction ( .995 ) );
			action.add( new MoveAction() );
			
			// Creating the stack for particles initialization
			var initializer : StackInitializer = new StackInitializer();
			initializer.add( new LifeInitializer ( 3000, 1000 ) );
			initializer.add( new ExplosionInitializer ( 200, new Range ( Math.PI - .1, Math.PI + .1 ), 50 ) );
			
			// Creating the infinite emission
			var emission : ByRateEmission = new ByRateEmission ( Particle, new Point( 300, 350 ), 100 );
			
			// Creating the emitter with stacks and launch the emission
			_system = new SimpleParticleSystem( initializer, action );
			_system.emit( emission ); 
			
			// Adding the draw routine
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );
		}
		public function enterFrameHandler ( e : Event ) : void
		{
			// Clear the scene
			graphics.clear();
			graphics.lineStyle( 2, 0xffffff );

			// Get an iterator of all particles of the scene
			var i : Iterator = ParticleManager.getInstance().getParticles().iterator();
			var p : Particle;
					
			// For each particle in the manager we draw a line of the current move	
			while( i.hasNext() )
			{
				p = i.next() as Particle;
				
				graphics.moveTo( p.lastPosition.x, p.lastPosition.y );
				graphics.lineTo( p.position.x, p.position.y );
			}
		}
	}
}